package com.example.jbmsuppliers.sinopec.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.activity.MyGoods;
import com.example.jbmsuppliers.sinopec.adapter.MyFragmentAdapter_lz_goods;
import com.example.jbmsuppliers.sinopec.net.NetDeceVersion;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.example.jbmsuppliers.sinopec.view.DoubleButtonDialog;
import com.example.permission.myjbm.model.MyFragmentModel;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jbm.jbmsupplier.util.IInformationCtrl;
import com.jbm.jbmsupplier.util.InfomationEntity;
import com.jbm.jbmsupplier.util.enums.BusinessStatus;
import com.jbm.jbmsupplier.util.enums.InfoStatus;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by 家帮忙 on 2017/3/2.
 */

public class ListFragmentGoods extends Fragment implements MyGoods.getDateType, MyFragmentAdapter_lz_goods.InformUpdate {
    @InjectView(R.id.pull_refresh_list)
    PullToRefreshListView pullRefreshList;
    @InjectView(R.id.lv_default1)
    ImageView lvDefault1;
    private boolean isVisibleToUser1;
    private final int GOODSUSER = 0001;
    private NetDeceVersion netdece;
    private int num = 5;
    private int s;
    private DialogLoading dialacitons;
    private static int su;
    private SimpleDateFormat df;
    private MyFragmentModel model;
    int page=0;
    private List<InfomationEntity> arr1;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        dialacitons = new DialogLoading(getContext(), "加载中...", true);
        View view = inflater.inflate(R.layout.lz_frement, container, false);
        arr1=new ArrayList<InfomationEntity>();
        page=0;
        ButterKnife.inject(this, view);
        df = new SimpleDateFormat("yyyy-MM-dd-HH-mm", Locale.CHINA);
        model=new MyFragmentModel();
        netdece = new NetDeceVersion(handler);
     //   if (isVisibleToUser1 == true)
    //        netdece.setGoods(InfoStatus.Normal.toValue(), page, num, GOODSUSER);

        PulluptpRegresh();
        if(su==0){
            netdece.setGoods(InfoStatus.Normal.toValue(), page, num, GOODSUSER);
        }

        return view;
    }

    private void PulluptpRegresh() {
        pullRefreshList.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                page+=5;
                if (dialacitons != null) {
                    dialacitons.show();
                }
                if (su == 0) {
                    netdece.setGoods(InfoStatus.Normal.toValue(), page, num, GOODSUSER);
                }
//        else
//        if (i == 1) {
//            netdece.setGoods(BusinessStatus.NotAudit.toValue(), page, num, GOODSUSER);
//        }
                else if (su == 2) {
                    netdece.setGoods(InfoStatus.Draft.toValue(), page, num, GOODSUSER);

                }

            }
        });

        pullRefreshList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page=0;
                if (dialacitons != null) {
                    dialacitons.show();
                }
                if (su == 0) {
                    netdece.setGoods(InfoStatus.Normal.toValue(), page, num, GOODSUSER);
                }
//        else
//        if (i == 1) {
//            netdece.setGoods(BusinessStatus.NotAudit.toValue(), page, num, GOODSUSER);
//        }
                else if (su == 2) {
                    netdece.setGoods(InfoStatus.Draft.toValue(), page, num, GOODSUSER);

                }

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page+=5;
                if (dialacitons != null) {
                    dialacitons.show();
                }
                if (su == 0) {
                    netdece.setGoods(InfoStatus.Normal.toValue(), page, num, GOODSUSER);
                }
//        else
//        if (i == 1) {
//            netdece.setGoods(BusinessStatus.NotAudit.toValue(), page, num, GOODSUSER);
//        }
                else if (su == 2) {
                    netdece.setGoods(InfoStatus.Draft.toValue(), page, num, GOODSUSER);

                }

            }
        });
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisibleToUser1 = isVisibleToUser;
    }


    private MyFragmentAdapter_lz_goods adapter;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GOODSUSER: {
                    IInformationCtrl arr = (IInformationCtrl) msg.obj;
                    if (arr.back) {
                        for (int i=0;i<arr.infomationList.size();i++){
                            model.modifyDate(arr.infomationList.get(i),df);
                        }
                        if (page == 0) {
                            arr1.clear();
                            arr1.addAll(arr.infomationList);
                            adapter = new MyFragmentAdapter_lz_goods(arr1, getActivity(), "", ListFragmentGoods.this,df);
                            pullRefreshList.setAdapter(adapter);
                        } else {
                            arr1.addAll(arr.infomationList);
                            adapter.setNotify(arr1);
                        }
                        if (arr1.size() == 0) {
                            lvDefault1.setVisibility(View.VISIBLE);
                        } else {
                            lvDefault1.setVisibility(View.GONE);
                        }
                        if (dialacitons != null)
                            dialacitons.dismiss();
                    } else {
                        if (dialacitons != null)
                            dialacitons.dismiss();
                        final DoubleButtonDialog dialog = new DoubleButtonDialog(
                                getActivity(), arr.errorinfo);
                        dialog.getokbtn().setOnClickListener(new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();

                            }
                        });
                        dialog.setisVisibility();
                    }
                    pullRefreshList.onRefreshComplete();
                }
            }


            super.handleMessage(msg);
        }
    };


    @Override
    public void setType(int i) {
        Log.e("main","执行"+i);
        page=0;
       // if (isVisibleToUser1 == true)
            su = i;
        if (i == 0) {
            netdece.setGoods(InfoStatus.Normal.toValue(), page, num, GOODSUSER);
        }
//        else
//        if (i == 1) {
//            netdece.setGoods(BusinessStatus.NotAudit.toValue(), page, num, GOODSUSER);
//        }
        else if (i == 2) {
            netdece.setGoods(InfoStatus.Draft.toValue(), page, num, GOODSUSER);

        }

    }

    @Override
    public void MyLissueUpdate(int i) {
        //  Draft (0), //草稿
        //   Normal(1), // 审核通过
        //   NotAudit (2), //待审核
        page=0;
        s = i;
        if (i == 1) {
            netdece.setGoods(BusinessStatus.Normal.toValue(), page, num, GOODSUSER);
        } else if (i == 2) {
            netdece.setGoods(BusinessStatus.NotAudit.toValue(), page, num, GOODSUSER);
        }
        else if (i == 0) {
            netdece.setGoods(BusinessStatus.Refuse.toValue(), page, num, GOODSUSER);

        }
    }

    @Override
    public void MyLlactionUpdate() {

    }
}
